gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\Classification_SVM_SteveGunn.m

    % 支持向量机用于二类模式分类 - 必须选择最优参数 p1,C
% 工具箱:SVM_SteveGunn
% 使用平台:Matlab6.5
% 作者:陆振波,海军工程大学
% 欢迎同行来信交流与合作,更多文章与程序下载请访问我的个人主页
% 电子邮件:luzhenbo@sina.com
% 个人主页:luzhenbo.88uu.com.cn

clc
clear
close all

%---------------------------------------------------
% 产生训练样本与测试样本
% 特别注意:此工具箱用于分类时,只能处理2类分类,且目标值必须为 1 或 -1。

n1 = [rand(3,5),rand(3,5)+1];
x1 = [1*ones(1,5),-1*ones(1,5)];

n2 = [rand(3,5),rand(3,5)+1];
x2 = [1*ones(1,5),-1*ones(1,5)];

xn_train = n1;          % 训练样本,每一列为一个样本
dn_train = x1;          % 训练目标,行向量

xn_test = n2;           % 测试样本,每一列为一个样本
dn_test = x2;           % 测试目标,行向量

%---------------------------------------------------
% 参数设置

trnX = xn_train';
trnY = dn_train';
tstX = xn_test';
tstY = dn_test';

ker = 'rbf';        % 核函数 k = exp(-(u-v)*(u-v)'/(2*p1^2))
global p1 ;
p1 = 5;             % p1 is width of rbfs (sigma)
C = 300;             % 折衷系数

%---------------------------------------------------
% 训练与测试

[nsv,alpha,bias] = svc(trnX,trnY,ker,C);                        % 训练

actfunc = 0;                                                    % 1 为实际输出,0 为取sign输出 
predictedY = svcoutput(trnX,trnY,tstX,ker,alpha,bias,actfunc);  % 测试

%---------------------------------------------------
% 结果统计

Result = ~abs(predictedY-tstY)               % 正确分类显示为1
Percent = sum(Result)/length(Result)   % 正确分类率